package coml.jiyun.hp.jiaohuafei.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import java.util.ArrayList;
import java.util.List;
import coml.jiyun.hp.jiaohuafei.R;
import coml.jiyun.hp.jiaohuafei.bean.ExchangeBean;

public class ExchangeAdapter extends RecyclerView.Adapter<ExchangeAdapter.ViewHolder> {
    private static final String TAG = "ExchangeAdapter";
    public List<ExchangeBean.DataBean.ListBean> list = new ArrayList<>();
    private Context context;
    public int asd=-1;

    public ExchangeAdapter(Context context) {
        this.context = context;
    }

    public void setList(List<ExchangeBean.DataBean.ListBean> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_exchange, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final ExchangeBean.DataBean.ListBean listBean = list.get(position);
        holder.tv_name.setText(listBean.getName());
        holder.tv_price.setText(String.valueOf(listBean.getPrice())+"元");
        holder.tv_sellCount.setText("销售："+String.valueOf(listBean.getSellCount())+"个");
        holder.tv_stockCount.setText("库充："+String.valueOf(listBean.getStockCount())+"个");
        holder.rb.setChecked(listBean.isSelected());
        Glide.with(context)
                .load(listBean.getPic())
                .apply(new RequestOptions().circleCrop())
                .into(holder.img_image);
				
        holder.rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (compoundButton.isPressed()){
                    asd=position;
                    listBean.setSelected(b);
                    for (int i = 0; i <list.size(); i++) {
                        ExchangeBean.DataBean.ListBean listBean1 = list.get(i);
                        if (i!=position){
                            listBean1.setSelected(false);
                        }else {
                            listBean1.setSelected(true);
                        }
                    }
                    notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public RadioButton rb;
        public ImageView img_image;
        public TextView tv_name;
        public TextView tv_stockCount;
        public TextView tv_sellCount;
        public TextView tv_price;
        public ViewHolder(View itemView) {
            super(itemView);
            this.rb = (RadioButton) itemView.findViewById(R.id.rb);
            this.img_image = (ImageView) itemView.findViewById(R.id.img_image);
            this.tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            this.tv_stockCount = (TextView) itemView.findViewById(R.id.tv_stockCount);
            this.tv_sellCount = (TextView) itemView.findViewById(R.id.tv_sellCount);
            this.tv_price = (TextView) itemView.findViewById(R.id.tv_price);
        }
    }
}
